﻿/****************************************************************************
Copyright (c) 2014-2015 凌惊雪 微信:Lingjingxue 邮箱:lingjingxue@sina.com QQ:271487457

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/

using UtilLib;
using ServerBase.Protocol;
using ServerBase.Config;
using ServerBase.LunarSocket;
using System.Collections.Generic;
using static ServerBase.Service.BaseDispatch;
using static ServerBase.Config.Conf;
using UtilPublic.ServerLoger;
using UtilPublic.Security;
using ServerGame.Service;
using ServerBase.Other;
using System;
using System.Threading;
using ServerBase.Service;
using static ServerGame.Service.Dispatcher;
using ServerBase.Client;
using static UtilPublic.Redis.ServerRedis;
using static ServerBase.DataBaseRedis.RdsTxt;
using static UtilPublic.Serialization.Jsh;
using ServerBase.IniFile;
using System.Text;
using static ServerBase.IniFile.IniConf;
using ServerPublic.Manager;
using ServerGame.GameObject;
using ServerPublic.Data;
using System.Linq;
using UtilPublic.Serialization;
using System.IO;
using System.Diagnostics;
using UtilPublic.ApiDispatcher;
using ServerGame.Data;

namespace ServerGame.Manager
{
    // 消息处理
    public partial class GmDispatcherMgr
    {
        #region 单例
        private static readonly GmDispatcherMgr _instance = new GmDispatcherMgr();
        public static GmDispatcherMgr Instance { get { return _instance; } }
        private GmDispatcherMgr() { } //私有构造方法(防止被外部实例化)
        #endregion

        #region 登录
        [ApiDpGm((int)EGtAction.登录)]
        public (EProtocolResult, string) 登录(string ReqGtArgs)
        {
            //在线人数
            var RspGtArgs = DataGame.DictPlayerOnline.Values.Count.ToString();
            loger.Info($"GM {ThisSession.Account} 登录");
            return (EProtocolResult.成功, RspGtArgs);
        }
        #endregion

        #region 服务器相关
        [ApiDpGm((int)EGtAction.服务器信息)]
        public (EProtocolResult, string) 服务器信息(string ReqGtArgs)
        {
            try
            {
                var RspArgs = new CLS_GtServerInfo()
                {
                    ConfigId = BaseServerInfo.ConfigId,
                    ProcessId = BaseServerInfo.ServerCurrentProcessID,
                    Cpu = BaseServerInfo.ServerCurrentProcessCpu,
                    Memory = BaseServerInfo.ServerCurrentProcessMemory,
                    Threads = BaseServerInfo.ServerCurrentProcessThreads,
                    RestrictLogin = BaseServerInfo.RestrictLogin,
                    CountOnline = DataGame.DictPlayerOnline.Values.Count,
                };
                return (EProtocolResult.成功, Jsh.Jstr(RspArgs));
            }
            catch
            {
                return (EProtocolResult.错误的修改参数, "");
            }
        }
        [ApiDpGm((int)EGtAction.服务器存档)]
        public (EProtocolResult, string) 服务器存档(string ReqGtArgs)
        {
            ServerMgrGame.ServerSave(true);
            return (EProtocolResult.成功, "");
        }
        [ApiDpGm((int)EGtAction.服务器关闭)]
        public (EProtocolResult, string) 服务器关闭(string ReqGtArgs)
        {
            ServerMgrGame.ServerExitPre();
            return (EProtocolResult.成功, "");
        }
        #endregion

        #region 游戏功能
        [ApiDpGm((int)EGtAction.上传配置文件)]
        public (EProtocolResult, string) 上传配置文件(string ReqGtArgs)
        {
            var GtArgs = Jsh.Jobj<CLS_GtUploadConfig>(ReqGtArgs);

            if (true)
            {
                string path_file = Conf.eConfJsonPath + "\\" + GtArgs.FileName;
                Util.DeleteFile(path_file);
                FileStream fs_class = new FileStream(path_file, FileMode.OpenOrCreate);
                StreamWriter sw_class = new StreamWriter(fs_class, Encoding.UTF8);
                sw_class.Write(GtArgs.FileText);
                sw_class.Close();
            }

            return (EProtocolResult.成功, "");
        }
        [ApiDpGm((int)EGtAction.更新配置文件)]
        public (EProtocolResult, string) 更新配置文件(string ReqGtArgs)
        {
            BaseCmds.Instance.重新读取配置文件指令();
            return (EProtocolResult.成功, "");
        }

        [ApiDpGm((int)EGtAction.临时走马灯)]
        public (EProtocolResult, string) 临时走马灯(string ReqGtArgs)
        {
            var GtArgs = Jsh.Jobj<CLS_GtNoticeRolling>(ReqGtArgs);
            NoticeMgr.GmNoticeRolling(GtArgs);
            return (EProtocolResult.成功, "");
        }
        [ApiDpGm((int)EGtAction.公告获取)]
        public (EProtocolResult, string) 公告获取(string ReqGtArgs)
        {
            var RspArgs = new CLS_GtNotice()
            {
                NoticeSystem = PubDataAllMgr.NoticeSystem,
                NoticeActivity = PubDataAllMgr.NoticeActivity,
            };
            return (EProtocolResult.成功, Jsh.Jstr(RspArgs));
        }
        [ApiDpGm((int)EGtAction.公告设置)]
        public (EProtocolResult, string) 公告设置(string ReqGtArgs)
        {
            var GtArgs = Jsh.Jobj<CLS_GtNotice>(ReqGtArgs);
            PubDataAllMgr.NoticeSystem = GtArgs.NoticeSystem;
            PubDataAllMgr.NoticeActivity = GtArgs.NoticeActivity;

            return (EProtocolResult.成功, "");
        }
        [ApiDpGm((int)EGtAction.临时走马灯未发送列表)]
        public (EProtocolResult, string) 临时走马灯未发送列表(string ReqGtArgs)
        {
            return (EProtocolResult.成功, Jsh.Jstr(NoticeMgr.DictNoticeRollingGm));
        }
        [ApiDpGm((int)EGtAction.临时走马灯未发送删除)]
        public (EProtocolResult, string) 临时走马灯未发送删除(string ReqGtArgs)
        {
            NoticeMgr.DictNoticeRollingGm.Remove(ReqGtArgs.ToLong());
            return (EProtocolResult.成功, Jsh.Jstr(NoticeMgr.DictNoticeRollingGm));
        }
        [ApiDpGm((int)EGtAction.服务器全局配置获取)]
        public (EProtocolResult, string) 服务器全局配置获取(string ReqGtArgs)
        {
            var RspArgs = new Dictionary<string, string>();
            RspArgs["服务器全局配置示例"] = DataGame.GameDemo;

            return (EProtocolResult.成功, Jsh.Jstr(RspArgs));
        }
        [ApiDpGm((int)EGtAction.服务器全局配置设置)]
        public (EProtocolResult, string) 服务器全局配置设置(string ReqGtArgs)
        {
            var GtArgs = Jsh.Jobj<Dictionary<string, string>>(ReqGtArgs);
            foreach (var kvp in GtArgs)
            {
                switch (kvp.Key)
                {
                    case "服务器全局配置示例": DataGame.GameDemo = kvp.Value; break;

                    default: break;
                }
            }
            return (EProtocolResult.成功, "");
        }
        [ApiDpGm((int)EGtAction.发送邮件)]
        public (EProtocolResult, string) 发送邮件(string ReqGtArgs)
        {
            var GtArgs = Jsh.Jobj<CLS_GtMail>(ReqGtArgs);
            if (GtArgs.MailType == EMailType.GM全服邮件)
            {
                //保存邮件数据
                var gmail = GtArgs.MailCreate();
                DataGame.GmailSave(gmail);
            }
            else
            {
                foreach (var puid in GtArgs.ListTarget)
                {
                    var mail = GtArgs.MailCreate();
                    MailMgr.MailToPlayer(puid, mail);
                }
            }
            return (EProtocolResult.成功, "");
        }
        [ApiDpGm((int)EGtAction.全局邮件列表)]
        public (EProtocolResult, string) 全局邮件列表(string ReqGtArgs)
        {
            var RspArgs = new Dictionary<long, CLS_GtMail>();
            foreach (var item in DataGame.DictGlobalMails.Values)
            {
                var node = item.MailInfoGtMail();
                RspArgs[node.Uid] = node;
            }
            return (EProtocolResult.成功, Jsh.Jstr(RspArgs));
        }
        [ApiDpGm((int)EGtAction.删除全服邮件根据ID)]
        public (EProtocolResult, string) 删除全服邮件根据ID(string ReqGtArgs)
        {
            DataGame.GmailRemove(ReqGtArgs.ToLong());

            var RspArgs = new Dictionary<long, CLS_GtMail>();
            foreach (var item in DataGame.DictGlobalMails.Values)
            {
                var node = item.MailInfoGtMail();
                RspArgs[node.Uid] = node;
            }
            return (EProtocolResult.成功, Jsh.Jstr(RspArgs));
        }
        [ApiDpGm((int)EGtAction.服务器限制登陆公告获取)]
        public (EProtocolResult, string) 服务器限制登陆公告获取(string ReqGtArgs)
        {
            return (EProtocolResult.成功, PubDataAllMgr.RestrictLogin);
        }
        [ApiDpGm((int)EGtAction.服务器限制登陆公告设置)]
        public (EProtocolResult, string) 服务器限制登陆公告设置(string ReqGtArgs)
        {
            PubDataAllMgr.RestrictLogin = ReqGtArgs;
            return (EProtocolResult.成功, "");
        }
        [ApiDpGm((int)EGtAction.服务器登陆限制设置)]
        public (EProtocolResult, string) 服务器登陆限制设置(string ReqGtArgs)
        {
            BaseServerInfo.RestrictLogin = true;
            Util.Show($"服务器登陆限制设置");
            return (EProtocolResult.成功, "");
        }
        [ApiDpGm((int)EGtAction.服务器登陆限制解除)]
        public (EProtocolResult, string) 服务器登陆限制解除(string ReqGtArgs)
        {
            BaseServerInfo.RestrictLogin = false;
            Util.Show($"服务器登陆限制解除");
            return (EProtocolResult.成功, "");
        }
        #endregion

        #region 游戏临时功能
        #endregion

        #region 游戏数据查询
        #endregion

    }
}